package com.stepforward.me;


import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.ZoomControls;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;



public class mapViewer extends MapActivity {
	static final int INITIAL_ZOOM_LEVEL = 15;
	
	static final double INITIAL_LATITUDE = 40.447468;
	static final double INITIAL_LONGITUDE = -79.942352;
	MapView mapView;
	List<Overlay> mapOverlays;
	Drawable drawable;
	ItemizedOverlayer itemizedOverlay;
        LinearLayout linearLayout;
        ZoomControls mZoom;
        int lati,longi;
        GeoPoint p;
        EventDataSQLHelper eventsData;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);

        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
      
        eventsData = new EventDataSQLHelper(this);
        
        mapOverlays = mapView.getOverlays();
        drawable = this.getResources().getDrawable(R.drawable.androidmarker);
        itemizedOverlay = new ItemizedOverlayer(drawable);
        p = new GeoPoint(
                (int) (INITIAL_LATITUDE * 1E6),
                (int) (INITIAL_LONGITUDE * 1E6));
        MapController mc = mapView.getController();
        mc.setZoom(INITIAL_ZOOM_LEVEL);
        mc.setCenter(p);
        
		Bundle extras = getIntent().getExtras();
		
		String departure = extras.getString("departure").replace("&", "AT");
		String arrival = extras.getString("arrival").replace("&", "AT");
		
        Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
        List<Address> addresses;
		try {
			   
			addresses = geoCoder.getFromLocationName(
					departure + " , Pittsburgh, PA", 5);
	
            GeoPoint g = null;
            if (addresses.size() > 0) {
                g = new GeoPoint(
                        (int) (addresses.get(0).getLatitude() * 1E6),
                        (int) (addresses.get(0).getLongitude() * 1E6));
                OverlayItem overlayitem = new OverlayItem(g, "", "");
                itemizedOverlay.addOverlay(overlayitem);
                
        lati= (int)addresses.get(0).getLatitude();
        longi = (int)addresses.get(0).getLongitude();
                mc = mapView.getController();
                mc.setZoom(INITIAL_ZOOM_LEVEL);
                mc.setCenter(g);
            }
            	addresses = geoCoder.getFromLocationName(
    					arrival + " , Pittsburgh, PA", 5);
    	
          GeoPoint       g2 = null;
                if (addresses.size() > 0) {
                    g2 = new GeoPoint(
                            (int) (addresses.get(0).getLatitude() * 1E6),
                            (int) (addresses.get(0).getLongitude() * 1E6));
                    OverlayItem overlayitem = new OverlayItem(g2, "", "");
                    itemizedOverlay.addOverlay(overlayitem);
                    mapOverlays.add(itemizedOverlay);
            lati= (int)addresses.get(0).getLatitude();
            longi = (int)addresses.get(0).getLongitude();
               
            mapView.getOverlays().add(new DirectionPathOverlay(g, g2));
              //  Toast.makeText(mapViewer.this, departure + " " +g.getLatitudeE6()+"  " + g.getLongitudeE6(), Toast.LENGTH_SHORT).show();
               
            }
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Toast.makeText(mapViewer.this, departure,Toast.LENGTH_SHORT).show();
                /**
        GeoPoint point =new GeoPoint(35400000, 139450000); //new GeoPoint((int)(lati * 1E6),(int)(longi*1E6));
        OverlayItem overlayitem = new OverlayItem(point, "", "");
    
        GeoPoint point2 = new GeoPoint(35410000, 139460000);
        OverlayItem overlayitem2 = new OverlayItem(point2, "Sekai, konichiwa!", "I'm in Japan!");
        mapController.setCenter(point2);
        mapController.setZoom(INITIAL_ZOOM_LEVEL);
    
        itemizedOverlay.addOverlay(overlayitem);
        itemizedOverlay.addOverlay(overlayitem2);
        mapView.getOverlays().add(new DirectionPathOverlay(point, point2));
        mapOverlays.add(itemizedOverlay);
        
    **/   
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        // use layout to "inflate" the menu
        getMenuInflater().inflate(R.layout.menu, menu);
        // note the option "previous" is disable in the XML layout
        return true;
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

}
